﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace AimGameLibrary.GameObject
{
    public class GameAnimation2D : GameRectangle 
    {
        String textureName;
        FrameData MyFrameData;
        KeyFrameData MyKeyFrameData;

        int currentColum;
        int PositionColum;
        int PositionRow;
        //int currentRow;
        float currentTime;
        float TempElapeTime;

        int mygameint;

        public GameAnimation2D()
        {
            PositionColum = 0;
            PositionRow = 0;
        }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 1);
            textureName = (String)getdata.Items.GetValue(IndexData + 2);
            MyFrameData = (FrameData)getdata.Items.GetValue(IndexData + 3);
            MyKeyFrameData = (KeyFrameData)getdata.Items.GetValue(IndexData + 4);
            Listnameparent.Add(node.Data);
        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);

            MyrectanglE = UpdateAnimation(timespan);
        }

        public override void UpdateGameTime(GameTime gametime)
        {
            base.UpdateGameTime(gametime);

            MyrectanglE = UpdateAnimation(gametime.TotalGameTime);
        }

        Rectangle UpdateAnimation(TimeSpan MygameTime)
        {
            currentTime += (float)MygameTime.TotalMilliseconds - TempElapeTime;

            if (currentTime > 1000 / MyFrameData.FrameRate)
            {
                currentColum++;
                currentTime = 0;
            }

            if (Listparent.Count > 0)
            {
                if (Listparent[0].GetType() == typeof(GameInt))
                {
                    GameInt tempint = (GameInt)Listparent[0];
                    if (MyKeyFrameData.MyKeyFrame.Count > tempint.MyinT)
                        mygameint = tempint.MyinT;
                }
            }

            if (MyKeyFrameData.MyKeyFrame.Count > mygameint)
            {
                TempElapeTime = (float)MygameTime.TotalMilliseconds;

                if (currentColum > MyKeyFrameData.MyKeyFrame[mygameint].Count - 1)
                    currentColum = 0;

                PositionColum = MyKeyFrameData.MyKeyFrame[mygameint][currentColum] * (MyFrameData.Width / MyFrameData.CountColum);
                PositionRow = mygameint * (MyFrameData.Height / MyFrameData.CountRow);
            }

            return new Rectangle(PositionColum, PositionRow, MyFrameData.Width / MyFrameData.CountColum, MyFrameData.Height / MyFrameData.CountRow);
        }

    }
}
